package com.moyun.common.interceptor;

import org.apache.ibatis.cache.CacheKey;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.reflection.ArrayUtil;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Flux;

/**
 * @ClassName DesensitizationInterceptor
 * @Description mybatis拦截器实现数据脱敏
 * @Author yangboxuan
 * @Date 2022/5/14 19:35
 */
@Component("DesensitizationInterceptor")
@Intercepts({
        @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class,
                RowBounds.class, ResultHandler.class}),
        @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class,
                RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class}),})
public class DesensitizationInterceptor implements Interceptor {

    private static final Logger logger = LoggerFactory.getLogger(DesensitizationInterceptor.class);

    private final boolean desensitization = false;

    // 检查入参类型是否添加指定注解

    // 检查出餐类型是否添加指定注解

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        // Invocation 中的三个对象 Object target, Method method, Object[] args
        Object result = invocation.proceed();
        //System.out.println(ArrayUtil.toString(invocation.getArgs()));

        // 如果需要对结果脱敏，则执行
        if (desensitization) {
            // 先对Map进行处理
//            if (result != null && result instanceof Map) {
//                return this.desensitizationMap(result);
//            }
//
//            // 处理集合
//            if (result instanceof ArrayList<?>) {
//                List<?> list = (ArrayList<?>) result;
//                return this.desensitization(list);
//            }
//
//            // 处理单个bean
//            return this.desensitization(result);
        }

        return result;
    }

    private Object desensitization(Object result) {
        return null;
    }

}